﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApplLogic.ApplClasses;
using ApplLogic.DbToApplConvertor;
using Entities;
using DAL;

namespace ApplLogic.ApplDbMediators
{
    public class ApplDbManagerMediator
    {
        private DataService m_DataService = DataService.Instance;
        private static volatile ApplDbManagerMediator s_Instance = null;
        private static object s_InstanceLockObj = new Object();

        private ApplDbManagerMediator() { }

        public static ApplDbManagerMediator Instance
        {
            get
            {
                if (s_Instance == null)
                {
                    lock (s_InstanceLockObj)
                    {
                        if (s_Instance == null)
                        {
                            s_Instance = new ApplDbManagerMediator();
                        }
                    }
                }

                return s_Instance;
            }
        }

        public void AddManager(ApplManager managerToAdd, bool saveChanges = false)
        {
            Manager dbManager = DbApplObjectConvertor.CreateManagerFromApplManager(managerToAdd);
            m_DataService.AddManager(dbManager, saveChanges);
        }

        public void DeleteManager(string managerId, bool saveChanges = false)
        {
            m_DataService.DeleteManager(managerId, saveChanges);
        }

        public ApplManager GetManager(string managerId)
        {
            Manager dbmanager = m_DataService.GetManagerByID(managerId);
            return DbApplObjectConvertor.CreateApplManagerFromDbManager(dbmanager);
        }

        public ApplManager GetManager(string userName, string password)
        {
            Manager dbManager = m_DataService.GetManagerByUserNameAndPassword(userName, password);
            return dbManager != null ? DbApplObjectConvertor.CreateApplManagerFromDbManager(dbManager) : null;
        }

        public void UpdateManagerName(string id, string newName, bool saveChanges = false)
        {
            m_DataService.UpdateManagerName(id, newName, saveChanges);
        }

        public void UpdateManagerLastName(string id, string newLastName, bool saveChanges = false)
        {
            m_DataService.UpdateManagerLastName(id, newLastName, saveChanges);
        }

        public void UpdateManagerEmailAdress(string id, string emailAdress, bool saveChanges = false)
        {
            m_DataService.UpdateManagerEmailAdress(id, emailAdress, saveChanges);
        }

        public void UpdateManagerToUpdateBirthDate(string id, DateTime newBirthDate, bool saveChanges = false)
        {
            m_DataService.UpdateManagerToUpdateBirthDate(id, newBirthDate, saveChanges);            
        }

        public void UpdateManagerUserName(string id, string newUserName, bool saveChanges = false)
        {
            m_DataService.UpdateManagerUserName(id, newUserName, saveChanges);                 
        }

        public void UpdateManagerPassword(string id, string newPassword, bool saveChanges = false)
        {
            m_DataService.UpdateManagerPassword(id, newPassword, saveChanges);                             
        }

        public void UpdateManager(ApplManager updatedApplManager)
        {
            UpdateManagerName(updatedApplManager.PersonalDetails.Id, updatedApplManager.PersonalDetails.FirstName);
            UpdateManagerPassword(updatedApplManager.PersonalDetails.Id, updatedApplManager.PersonalDetails.Password);
            UpdateManagerUserName(updatedApplManager.PersonalDetails.Id, updatedApplManager.PersonalDetails.UserName);
            UpdateManagerToUpdateBirthDate(updatedApplManager.PersonalDetails.Id, updatedApplManager.PersonalDetails.BirthDate);
            UpdateManagerEmailAdress(updatedApplManager.PersonalDetails.Id, updatedApplManager.PersonalDetails.EmailAdress);
            UpdateManagerLastName(updatedApplManager.PersonalDetails.Id, updatedApplManager.PersonalDetails.LastName);
            UpdateManagerName(updatedApplManager.PersonalDetails.Id, updatedApplManager.PersonalDetails.FirstName);
            m_DataService.SaveChanges();
        }
    }
}
